package com.zifan.st_geometry.mapper;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.type.ClobTypeHandler;

import com.zifan.st_geometry.model.STGeometry;

@Mapper
public interface STGeometryMapper {

	final static String GET_NEXT_OBJECTID_SQL = "SELECT SDE.VERSION_USER_DDL.NEXT_ROW_ID(UPPER(#{owner,typeHandler=com.zifan.st_geometry.handler.DBOwnerHandler}),(SELECT REGISTRATION_ID FROM SDE.TABLE_REGISTRY WHERE TABLE_NAME = UPPER(#{tableName}) AND OWNER = UPPER(#{owner,typeHandler=com.zifan.st_geometry.handler.DBOwnerHandler}))) AS OBJECTID FROM DUAL";

	/**
	 * 
	 * @param srcTableName
	 * @param targetTableName
	 * @param fields
	 * @param where
	 * @param srId
	 * @return
	 */
	@Results({
			@Result(property = "OVERLAPCOORDS", column = "OVERLAPCOORDS", javaType = String.class, typeHandler = ClobTypeHandler.class) })
	@SelectProvider(type = STGeomertySqlBuilder.class, method = "buildQueryIntersectByWhere")
	List<HashMap<String, Object>> queryIntersectByWhere(String srcTableName, String targetTableName, String[] fields,
			String where, @Param("srId") int srId);

	@Results({
			@Result(property = "OVERLAPCOORDS", column = "OVERLAPCOORDS", javaType = String.class, typeHandler = ClobTypeHandler.class) })
	@SelectProvider(type = STGeomertySqlBuilder.class, method = "buildQueryIntersectByWKT")
	List<HashMap<String, Object>> queryIntersectByWKT(String targetTableName, String[] fields, @Param("wkt") String wkt,
			@Param("srId") int srId);

	@SelectKey(statement = GET_NEXT_OBJECTID_SQL, keyProperty = "objectId", before = true, resultType = long.class)
	@InsertProvider(type = STGeomertySqlBuilder.class, method = "buildInsertSql")
	void saveGeometryToTable(STGeometry object);

	@SelectProvider(type = STGeomertySqlBuilder.class, method = "builderGetCountSql")
	int getCountByTableName(@Param("tableName") String tableName);

}
